package com.gitee.melin.bee.core.jdbc.dialect.impl;

import com.gitee.melin.bee.core.jdbc.enums.DataSourceType;
import com.gitee.melin.bee.core.jdbc.relational.ConnectionInfo;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Gbase8aJdbcDialect extends MySqlJdbcDialect {

    protected static final List<String> EXCLUDED_SCHEMAS = Collections.unmodifiableList(
            Arrays.asList("information_schema", "mysql", "performance_schema", "metrics_schema"));

    @Override
    public DataSourceType getDataSourceType() {
        return DataSourceType.GBASE8A;
    }

    private static final Logger LOG = LoggerFactory.getLogger(Gbase8aJdbcDialect.class);

    public Gbase8aJdbcDialect(ConnectionInfo connectionInfo) {
        super(connectionInfo);
    }

    public Gbase8aJdbcDialect(Connection connection) {
        super(connection);
    }

    @Override
    protected Driver getDriver() throws SQLException {
        return loadDriver(DataSourceType.GBASE8A.getDriverClass());
    }

    @Override
    protected boolean filterSchema(String schemaName) {
        return !EXCLUDED_SCHEMAS.contains(schemaName);
    }

    @Override
    protected String getSelectColumnsSql() {
        return "select\n"
                + "    a.COLUMN_NAME,\n"
                + "    a.COLUMN_TYPE,\n"
                + "    a.DATA_TYPE,\n"
                + "    a.COLUMN_COMMENT,\n"
                + "    a.COLUMN_DEFAULT AS DEFAULT_VALUE,\n"
                + "    a.IS_NULLABLE,\n"
                + "    a.NUMERIC_PRECISION,\n"
                + "    a.NUMERIC_SCALE,\n"
                + "    a.CHARACTER_OCTET_LENGTH DATA_LENGTH,\n"
                + "    null AS DATETIME_PRECISION\n"
                + "from INFORMATION_SCHEMA.COLUMNS a\n"
                + "where a.TABLE_SCHEMA = :schemaName and a.TABLE_NAME = :tableName\n"
                + "order by a.ORDINAL_POSITION ASC";
    }
}
